#include "threadPool.h"
#include "log.h"
#include "transfile.h"

int userInit(char *path);

int makeWorker(threadPool_t *threadPool)
{

    for(int i = 0; i < threadPool->worker_num; ++i)
    {
        pthread_create(&threadPool->tid_arr[i],NULL,threadFunc,threadPool);
    }
    return 0;
}

void *threadFunc(void *arg)
{
    char real_path[4000] = {0};

    threadPool_t *threadPool = (threadPool_t *)arg;
    while(1)
    {
        // 取出任务
        pthread_mutex_lock(&threadPool->task_queue.mutex);
        while(threadPool->exit_flag == 0 && threadPool->task_queue.queue_size == 0)
        {
            pthread_cond_wait(&threadPool->task_queue.cond, &threadPool->task_queue.mutex);
        }
        if(threadPool->exit_flag != 0)
        {
            printf("child is going to exit!\n");
            pthread_mutex_unlock(&threadPool->task_queue.mutex);
            pthread_exit(NULL);
        }
        printf("tid = %lu\n",pthread_self());
        printf("child get a task!\n");
        int netfd = threadPool->task_queue.head->netfd;
        //printf("netfd = %d\n",netfd);
        dequeue(&threadPool->task_queue);
        pthread_mutex_unlock(&threadPool->task_queue.mutex);

        // 任务区
        char username[1024] = {0};
        char password[1024] = {0};
        int virtual_start = 0;
        train_t train;

        if(threadPool->password == 0)
        {
            //int ret = test2(netfd);
            int length;
            recvn(netfd,&length,sizeof(int));
            recvn(netfd,username,length);
            //printf("username: %s\n",username);

            recvn(netfd,&length,sizeof(int));
            recvn(netfd,password,length);
            //printf("text: %s\n", password);

            //密码验证
            int ret = confirmPasswd(username,password);
            //printf("ret = %d\n",ret);
            if(ret == 1)//密码正确
            {
                InitLog("log");
                char buf[1024] = {0};
                getcwd(buf,sizeof(buf));
                sprintf(real_path,"%s%s%s",buf,"/user/",username);
                userInit(real_path);
                send(netfd,&ret,sizeof(int),MSG_NOSIGNAL);
                threadPool->password = 1;
                //登录成功记录
                //printf("%s\n",real_path);
                InitLog(username) ;
                printf("confirmPassword success!\n");
                virtual_start = strlen(real_path) - strlen(username) - 1;
            }
            else
            {
                //printf("fail ret = %d\n",ret);
                send(netfd,&ret,sizeof(int),MSG_NOSIGNAL);
            }
        }

        while(threadPool->password == 1) 
        {
            char mkrm_path[4000] = {0};
            strcpy(mkrm_path,real_path);
            //printf("密码正确！\n");
            order_t order;
            //初始化order
            orderInit(&order,netfd);
            orderValue(&order,netfd);
            char buf[1024] = {0};
            //printf("command = %d,opt = %s\n",order.num,order.opt);
             
            //退出标志
            //if(order.num == 88)
            //{
            //    printf("tid = %lu exit\n",pthread_self());
            //    threadPool->password = 0;
            //    break;
            //}
            

            switch(order.num)
            {
            case EXIT:
                printf("tid = %lu exit\n",pthread_self());
                threadPool->password = 0;
                //transfile(netfd);
                close(netfd);
                // 恢复空闲
                printf("child finish a task!\n");

                break;

            case CD:
                //printf("real_path = %s\n",real_path);
                myCd(real_path,order.opt);
                NETDESK_LOG_INFO(username,"cd");
                printf("cd over\n");
                bzero(&train,sizeof(train));
                for(int i = virtual_start,j = 0;i < strlen(real_path);i++,j++)
                {
                    train.data[j] = real_path[i];
                }
                train.length = strlen(train.data);
                send(netfd,&train,sizeof(int) + train.length,MSG_NOSIGNAL);
                //InitLog("cd") ;
                //cd...
                //cd记录
                break;

            case LS:
                ls(netfd,real_path);
                NETDESK_LOG_INFO(username,"ls");
                printf("ls over\n");
                //ls...
                //记录.
                //InitLog("ls");
                break;

            case PUTS:
                printf("puts over\n");
                //printf("puts: order.num = %d, order.opt = %s\n",order.num,order.opt);
                putFile(order.opt, netfd, real_path);
                NETDESK_LOG_INFO(username,"puts");
                //puts...
                //记录 
                //InitLog("puts");
                break;

            case GETS:
                printf("gets over\n");
                //printf("get: order.num = %d, order.opt = %s\n",order.num,order.opt);
                getFile(order.opt, netfd, real_path);
                NETDESK_LOG_INFO(username,"gets");
                printf("finish\n");
                /* gets
                 * 先判断该文件大小是否>100M
                 * 小于用gets功能
                 * 大于用mmap功能
                 * 记录
                 * */
                //InitLog("gets");
                break;

            case REMOVE:
                mkrm_path[strlen(mkrm_path)] = '/';
                strcat(mkrm_path,order.opt);
                Remove(netfd, mkrm_path,order.opt);
                printf("remove over\n");
                //remove...
                //记录
                //InitLog("remove");
                break;

            case PWD:
                pwd(netfd,real_path,virtual_start);
                printf("pwd over\n");
                NETDESK_LOG_INFO(username,"pwd");
                //pwd...
                //记录
                //InitLog("pwd");
                break;

            case MKDIR:
                //strcat(buf1,real_path);
                //sprintf(real_path,"%s%s","/",buf1);
                mkrm_path[strlen(mkrm_path)] = '/';
                strcat(mkrm_path,order.opt);
                Mkdir(netfd,mkrm_path,order.opt);
                printf("mkdir over\n");
                //mkdir...
                //记录
                //InitLog("mkdir");
                break;

            case RMDIR:
                mkrm_path[strlen(mkrm_path)] = '/';
                strcat(mkrm_path,order.opt);
                //printf("rmdir = %s\n",mkrm_path);
                Rmdir(netfd,mkrm_path,order.opt);
                printf("rmdir over\n");
                //rmdir
                //记录
                //InitLog("rmdir");
                break;

            default :
                break;

            }
        }

        //transfile(netfd);
        close(netfd);
        // 恢复空闲
        printf("child finish a task!\n");
    }
}

int userInit(char * path)
{
    char buf[4096] = {0};
    DIR *pdir = opendir(path);
    //printf("userInit path = %s\n",path);

    if(pdir == NULL)
    {
        mkdir(path,0777);
    }

    closedir(pdir);

    return 0;
}
